package com.cn.项目.飞机预约管理系统.Logining;


import com.cn.项目.utils.JdbcUtils;

import javax.xml.transform.Result;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class AdminSever implements Serializable {
    ServerSocket serverSocket=null;
    int port;
    Socket socket;
    ObjectOutputStream os;
    ObjectInputStream oi;
    public AdminSever(int port) {
        try {
            this.serverSocket=new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        AdminSever AdminSever = new AdminSever(10099);
        while (true){
            try {
                AdminSever.setSocket(AdminSever.getServerSocket().accept());
                AdminSever.getIO();
                Admin admin=(Admin) AdminSever.oi.readObject();
                boolean b=AdminSever.seltAdmin(admin);
                if (b){
                    String str="true";
                    AdminSever.os.writeObject(str);
                }else{
                    String str="false";
                    AdminSever.os.writeObject(str);
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    //管理员登录
    public boolean seltAdmin(Admin admin){
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        ResultSet resultSet = null;
        boolean flag;
        try {
            connection = JdbcUtils.getConnection();
            String SQL = "select * from Admin where number =? and password =?";
            preparedStatement = connection.prepareStatement(SQL);
            preparedStatement.setObject(1,admin.getAdminName());
            preparedStatement.setObject(2,admin.getPwd());
            resultSet = preparedStatement.executeQuery();
            flag = preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (preparedStatement.execute())return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    //获得IO流
    public void getIO(){
        try {
            oi=new ObjectInputStream(socket.getInputStream());
            os=new ObjectOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //关闭流操作
    public void closeIO(){
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            oi.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public ServerSocket getServerSocket() {
        return serverSocket;
    }

    public void setServerSocket(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Socket getSocket() {
        return socket;
    }


    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public ObjectOutputStream getOs() {
        return os;
    }

    public void setOs(ObjectOutputStream os) {
        this.os = os;
    }

    public ObjectInputStream getOi() {
        return oi;
    }

    public void setOi(ObjectInputStream oi) {
        this.oi = oi;
    }
}
